require "proto/scene/message/ReqSceneLoadOverMessage"
require "proto/scene/message/ReqPkMessage"
require "proto/scene/message/ReqMoveMessage"
require "proto/scene/message/ReqEmojMessage"
require "proto/scene/message/ReqGatherMessage"
require "proto/scene/message/ReqAtkStoneMessage"
---@class RedRob.BattleMsgHandler
local BattleMsgHandler = RedRob.CreateSingleton("BattleMsgHandler")
local this = BattleMsgHandler
--region res
---返回场景中的视野对象信息
---@param msg ResSceneObjMessage
function BattleMsgHandler.ResSceneObjHandler(msg)

    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart or RedRob.ClientBattle.isRoomDestroy then
        return
    end
    RedRob.ReportMgr.RecordMsg(msg,"ResScene")
    local aMgr = RedRob.ActorManager
    for k,v in pairs(msg.onSeePlayers) do
        local id = tostring(v.playerId)
        if id == logicMgr.HeroManager.GetRoleId() then
            RedRob.KeyController.SetCtrlId(id)
        end
        local actor = aMgr.GetActorInAll(id)
        if actor then
            aMgr.UpdateNetPlayer(actor,v)
            aMgr.ActorInScene(actor)
        else
            actor = aMgr.CreateNetPlayer(v)
        end
        if actor:GetId() == logicMgr.HeroManager.GetRoleId() then
            --RedRob.KeyController.SetCtrl(actor)
            RedRob.CameraController.SetFollowTarget(actor)
        end
    end
    for k,v in pairs(msg.onSeeMinerals) do
        local id = tostring(v.objId)
        local actor = aMgr.GetActorInAll(id)
        if actor then
            actor:SetPos(v.x,v.y)
            aMgr.ActorInScene(actor)
        else
            actor = aMgr.CreateNetMinerals(v)
        end
    end
    for k,v in pairs(msg.onSeeDarkStones) do
        local id = tostring(v.objId)
        local actor = aMgr.GetActorInAll(id)
        if actor then
            actor:SetPos(v.x,v.y)
            aMgr.ActorInScene(actor)
        else
            actor = aMgr.CreateNetStone(v)
            RedRob.GameMode.AddMidTip(v.modelId)
        end
    end
    --等玩家创建完成 再pk
    for k,v in pairs(msg.onSeePlayers) do
        if v.pkInfo then
            RedRob.ActorManager.UpdateNetPkInfo(v.pkInfo)
        end
    end
    for k,v in pairs(msg.removeObjs) do
        local id = tostring(v)
        aMgr.ActorOutScene(id)
    end
end
---返回场景中的对象移动消息
---@param msg ResMoveMessage
function BattleMsgHandler.ResMoveHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then
        return
    end
    RedRob.ReportMgr.RecordMsg(msg,"ResMove")
    local id = tostring(msg.playerId)
    local player = RedRob.ActorManager.GetActor(id)
    if not player then
        logError("no player "..id)
        return
    end
    if player == RedRob.KeyController.GetCtrl() then return end
    if player:IsInBattle() then
        return
    end
    player:MoveToDestination(msg.x,msg.y,msg.direction)
end
---主角色当前场景信息(比如登录成功后,服务端通知玩家开始加载地图)
---@param msg ResSceneInfoMessage
function BattleMsgHandler.ResSceneInfoHandler(msg)
    if msg.state ~= 0 then return end
    --RedRob.ReportMgr.RecordMsg(msg,"ResScene")
    RedRob.ClientBattle.Start(msg.sceneId,msg.x,msg.y)
end
---玩家停止移动
---@param msg ResStopMoveMessage
function BattleMsgHandler.ResStopMoveHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then
        return
    end
    RedRob.ReportMgr.RecordMsg(msg,"ResStopMove")
    local player = RedRob.ActorManager.GetActor(tostring(msg.playerId))
    if not player then
        logError("no player "..tostring(msg.playerId))
        return
    end
    player:MoveToDestination(msg.x,msg.y,msg.direction)
end
---玩家复活(其他玩家也会收到这个消息)
---@param msg ResRelifeMessage
function BattleMsgHandler.ResRelifeHandler(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ResRelife")
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then
        return
    end
    local player = RedRob.ActorManager.GetActor(tostring(msg.playerId))
    if not player then
        logError("no player "..tostring(msg.playerId))
        return
    end
    player:Reborn(msg.x,msg.y)
end
function BattleMsgHandler.ResRefreshTipsHandler(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ResRefreshTips")
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    RedRob.GameMode.UpdateResRefreshTip(msg.eventId,msg.type,msg.mineralId,msg.delaySeconds)
    FireEvent(RedRob.Config.Event.ResourceRefreshTip)
end
---玩家死亡
function BattleMsgHandler.ResDieHandler(msg)
end
---返回玩家PK状态
---@param msg ResPkInfoMessage
function BattleMsgHandler.ResPkInfoHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then
        return
    end
    RedRob.ReportMgr.RecordMsg(msg,"ResPkInfo")
    local info = msg.pkInfo
    RedRob.ActorManager.UpdateNetPkInfo(info)
    local id1 = tostring(info.playerId1)
    local id2 = tostring(info.playerId2)
    local winId = tostring(info.playerIdWin)
    RedRob.GameMode.SetAttrReduce(winId,msg.attrReduce)
    RedRob.GameMode.SetAttrReduce(winId == id1 and id2 or id1,0)
    FireEvent(RedRob.Config.Event.PlayerAttrReduce,tostring(info.playerIdWin),-msg.attrReduce)
end
---返回玩家身上当前的资源
function BattleMsgHandler.ResCurrMoneyHandler(msg)
    if msg.state ~= 0 then return end
    RedRob.ReportMgr.RecordMsg(msg,"ResCurrMoney")
    if not RedRob.ClientBattle.isStart then
        return
    end
    local id = tostring(msg.playerId)
    RedRob.GameMode.PlayerResourceUpdate(id,msg.currMoney)
    FireEvent(RedRob.Config.Event.PlayerResourceUpdate,id,msg.currMoney)
end
---返回当前场景的一些时间相关参数
function BattleMsgHandler.ResSceneTimeHandler(msg)
    if msg.state ~= 0 then return end
    RedRob.ReportMgr.RecordMsg(msg,"ResSceneTime")
    RedRob.ClientBattle.InitBattleTime(msg.closeTime,msg.countdownTime,msg.openTime)

end
---收到其他玩家发送的表情包
function BattleMsgHandler.ResEmojHandler(msg)
    if msg.state ~= 0 then return end
    RedRob.ReportMgr.RecordMsg(msg,"ResEmoj")
    local id = tostring(msg.playerId)
    FireEvent(RedRob.Config.Event.PlayerEmojiShow,id,msg.eomj)
end
---返回场景对象身上的buff变化
function BattleMsgHandler.ResBuffHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    local a = RedRob.ActorManager.GetActorInAll(tostring(msg.objId))
    if not a then return end
    if msg.actionType == 1 then
        a:AddBuff(msg.buffInfo.buffId,msg.buffInfo.overlay)
    else
        a:RemoveBuff(msg.buffInfo.buffId)
    end
    FireEvent(RedRob.Config.Event.BuffChanged,a)
end
---返回矿物的变化信息
function BattleMsgHandler.ResMineralChangeHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    RedRob.ReportMgr.RecordMsg(msg,"ResMineralChange")
    local id = tostring(msg.objId)
    local a = RedRob.ActorManager.GetActor(id)
    local status = 0
    if msg.status == 1 then
        status = RedRob.Config.MineralStatus.digEmpty
    else
        status = RedRob.Config.MineralStatus.full
    end
    if a.actorType == RedRob.Config.ActorType.minerals then
        a:PickUp(RedRob.ActorManager.GetActor(tostring(msg.playerId)))
    end
    FireEvent(RedRob.Config.Event.MineralReward,a,tostring(msg.playerId))
    logicMgr.DarkStreetMgr.Track_mining(id)
end
---返回临时背包的信息
function BattleMsgHandler.ResTempBagListHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    RedRob.GameMode.InitBagList(msg.itemList)
    RedRob.ReportMgr.RecordMsg(msg,"ResTempBagList")
end
---返回临时背包中的道具变化
function BattleMsgHandler.ResTempItemChangeHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    RedRob.GameMode.AddBag(msg.itemInfo)
    RedRob.ReportMgr.RecordMsg(msg,"ResTempItemChange")
end
---返回暗巷宝石被攻击信息
---@param msg ResAtkStoneMessage
function BattleMsgHandler.ResAtkStoneHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    local id = tostring(msg.objId)
    local attacker = RedRob.ActorManager.GetActor(tostring(msg.playerId))
    local defender = RedRob.ActorManager.GetActor(tostring(id))
    if msg.currHp == 0 then
        RedRob.GameMode.AddMidTip(defender:GetModelId(),true,tostring(msg.playerId))
    end
    if attacker then
        attacker:Fight(defender,msg.damage)
    else
        if defender then
            defender:Hurt(msg.currHp,attacker)
        end
    end

end
---返回暗巷宝石提示信息
function BattleMsgHandler.ResStoneTipsHandler(msg)
    if msg.state ~= 0 then return end
    if not RedRob.ClientBattle.isStart then return end
    RedRob.GameMode.UpdateResRefreshTip(msg.eventId,msg.type,nil,msg.delaySeconds,true)
end
--endregion res
--region req
---通知服务端,场景loading完毕
function BattleMsgHandler.ReqSceneLoadOver()
    local msg = ReqSceneLoadOverMessage.New()
    sendSceneReqMessage(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ReqSceneLoadOver")
end
function BattleMsgHandler.ReqMove(curX,curY,x,y)
    local msg = ReqMoveMessage.New()
    msg.currX = curX
    msg.currY = curY
    msg.x = x
    msg.y = y
    sendSceneReqMessage(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ReqMove")
end
---玩家发起PK
function BattleMsgHandler.ReqPk(playerId,x,y)

    local msg = ReqPkMessage.New()
    msg.playerId = playerId
    msg.x = x
    msg.y = y
    sendSceneReqMessage(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ReqPk")
end
---ai for debug test
function BattleMsgHandler.ReqPkByAi(attackId,defenderId)

    local msg = ReqPkMessage.New()
    msg.playerId = defenderId
    msg.attackId = attackId
    RedRob.ReportMgr.RecordMsg(msg,"ReqPkByAi")
    sendSceneReqMessage(msg)
end
function BattleMsgHandler.ReqMoveByAi(playerId,x,y)
    local msg = ReqMoveMessage.New()
    msg.playerId = playerId
    local actor = RedRob.ActorManager.GetActor(playerId)
    msg.currX = actor:GetPos().x
    msg.currY = actor:GetPos().y
    msg.x = x
    msg.y = y
    sendSceneReqMessage(msg)
end
---发送表情包
function BattleMsgHandler.ReqEmoji(name)
    local msg = ReqEmojMessage.New()
    msg.eomj = name
    sendSceneReqMessage(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ReqEmoji")
end
---采矿
function BattleMsgHandler.ReqGather(id)
    local msg = ReqGatherMessage.New()
    msg.objId = id
    sendSceneReqMessage(msg)
    RedRob.ReportMgr.RecordMsg(msg,"ReqGather")
end
---请求攻击暗巷宝石
function BattleMsgHandler.ReqAtkStone(id)
    local msg = ReqAtkStoneMessage.New()
    msg.objId = id
    sendSceneReqMessage(msg)
end
--endregion req



